package com.cssy.authority.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cssy.authority.dao.RoleDAO;
import com.cssy.authority.dao.impl.RoleDAOImpl;
import com.cssy.authority.entity.Role;
import com.cssy.authority.service.IRoleService;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.plat.exception.BaseException;



public class RoleServiceImpl implements  IRoleService{
	
	private RoleDAO roleDao;
	
	public RoleServiceImpl( SqlMapClient sqlMapClient){
		
		roleDao=new RoleDAOImpl(sqlMapClient);
	}

	@Override
	public List<Role> getRolesByPerId(String permissionid) throws BaseException {
		
		try {
			return roleDao.selectByExample(permissionid);
		} catch (SQLException e) {
			
			e.printStackTrace();
			throw new BaseException();
		}
	}

	public static Role domainToPojo(com.byd.acegiext.domain.Role role){
		Role rolePojo = new Role();
		rolePojo.setId(Long.parseLong(role.getId().toString()));
		rolePojo.setName(role.getName());
		rolePojo.setDescription(role.getDescription());
		rolePojo.setCompanyId(role.getCompanyId());
		return rolePojo;
	}
	public static List<Role> domainToPojo(List<com.byd.acegiext.domain.Role> roles){
		List<Role> rolePojos = new ArrayList<Role>();
		for(com.byd.acegiext.domain.Role tmp : roles){
			rolePojos.add(domainToPojo(tmp));
		}
		return rolePojos;
	}

	@Override
	public void updateRole(Role role) throws BaseException {
		try {
			roleDao.updateByPrimaryKeySelective(role);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
